﻿define([],function(){
	return {
		setCookie : function(key, val, expires){
			var now = new Date();
			now.setDate( now.getDate()+expires );
			document.cookie = key+"="+val+"; expires="+now;
		},
		getCookie : function(key){
			var cookiestr = document.cookie;
			var list = cookiestr.split("; ");
			for(var attr in list){
				var kv = list[attr].split("=");
				if(kv[0] == key){
					return kv[1];
				}
			}
			return "";
		},
		getpageCoord : function(ele){
			var totalX = 0;
			var totalY = 0;
			totalX += ele.offsetLeft;
			totalY += ele.offsetTop;
			while(ele.offsetParent){
				ele = ele.offsetParent;
				totalX += ele.offsetLeft;
				totalY += ele.offsetTop;
	}
	return {x:totalX, y:totalY};
		},
		getStyle : function(ele){
			//如果判断是IE？？
			if(ele.currentStyle){
				return ele.currentStyle;
			} else {
				return getComputedStyle(ele,null); // 
			}
		},
		getChildElements : function(ele){
			var newlist = [];
			for(var i=0; i<ele.childNodes.length; i++){
				if( ele.childNodes[i].nodeType == 1){
					newlist.push(ele.childNodes[i]);
				}
			}
			return newlist;
		},
		randomInt : function(min, max){
			return Math.round(Math.random()*(max-min)) + min;
		},
		randomColor : function(){
			var R = randomInt(0,255).toString(16);
			var G = randomInt(0,255).toString(16);
			var B = randomInt(0,255).toString(16);
			
			return "#" + (R.length<2?"0"+R:R) + (G.length<2?"0"+G:G) + (B.length<2?"0"+B:B);
		},
		getTarget : function(ele,sport){
			//获取需要到达相对于小球原点的位置
			var ballPoint={x:ele.offsetLeft,y:ele.offsetTop};
			var targetPoint={x:sport.left-ballPoint.x,y:sport.top-ballPoint.y}
			//y= ax*2+bx;需要求出b的值；需要指定一个a的值；
			var a=0.001;
			var b=(targetPoint.y-a*Math.pow(targetPoint.x,2))/targetPoint.x;
			//当前小球的位置；
			//计算y的值；
			var x=0
			//定义一个计时器；
			var t=setInterval(function(){
				x+=5;
				var y=a*x*x+b*x;
				
				//计算小球的位置；
				ele.style.left=x+ballPoint.x+"px";
				ele.style.top=y+ballPoint.y+"px";
				
				if(x>=targetPoint.x){
					clearInterval(t);
					ele.style.left=sport.left+"px";
					ele.style.top=sport.top+"px";
					return;
				}
			},10);
		},
		getCrash : function(ele,target){
			//只要上下左右都不接触；就不是碰撞；
			//控制的物体
			var eleTop = ele.offsetTop;
			var eleRight = ele.offsetLeft+ele.offsetWidth;
			var eleBottom = ele.offsetTop+ele.offsetHeight;
			var eleLeft = ele.offsetLeft;
			//要碰撞的物体
			var targetTop = target.offsetTop;
			var targetRight = target.offsetLeft+target.offsetWidth;
			var targetBottom = target.offsetTop+target.offsetHeight;
			var targetLeft = target.offsetLeft;
			//开始判断
			if(eleBottom<targetTop || eleRight<targetLeft || eleTop>targetBottom || eleLeft>targetRight){
				return false;
			}else{
				return true;
			}
		}
	}
})
	
